package org.example.config.autowire;

import org.example.config.autowire.fiegn.EnableFeignClient;
import org.example.config.autowire.fiegn.FeignClientProxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.support.AbstractApplicationContext;

import java.util.Scanner;

@EnableFeignClient
@SpringBootApplication
public class AutowiredApplication {
    public static void main(String[] args) {
        System.setProperty("weather.url", "http://localhost:9999");
        // AnnotationConfigServletWebServerApplicationContext cannot be cast to AnnotationConfigApplicationContext
        AbstractApplicationContext context = null;
        try {
            context = (AbstractApplicationContext) SpringApplication.run(AutowiredApplication.class, args);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        FeignClientProxy proxy = context.getBean(FeignClientProxy.class);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("please input the command (0 exit, 1 refresh):");
            Integer i = scanner.nextInt();
            if (i == 0) {
                try {
                    context.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            }
            if (i == 1) {
                // GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
                try {
                    context.refresh();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                System.out.println("not supported");
            }
        }
    }
}
